﻿Public Class HeroDataDesigner

    Private _heroDS As HeroDS

    Public Sub New()

        InitializeComponent()

        LoadCsv()
        AddHeroColumns()

    End Sub

#Region " Event Handlers "

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click

        LoadCsv()

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Dim writer As New CsvWriter(PrepHeroDSForWriter(_heroDS), PrepHeroDSHeadersForWriter(_heroDS))
        writer.Write(AppConstants.HeroDesignerCsvPath)

    End Sub

    Private Sub DgrHeroes_CellMouseClick(ByVal sender As Object, ByVal e As DataGridViewCellMouseEventArgs) Handles dgrHeroes.CellMouseClick
        If e.ColumnIndex > -1 AndAlso e.RowIndex > -1 Then
            dgrHeroes.BeginEdit(False)
        End If
    End Sub

#End Region

#Region " Private Methods "

    Private Sub LoadCsv()

        Dim reader As New CsvReader(AppConstants.HeroDesignerCsvPath)

        _heroDS = GetHeroDS(reader.Data)
        HeroBindingSource.DataSource = _heroDS
        HeroBindingSource.DataMember = "Hero"

    End Sub

    Private Function GetHeroDS(ByVal csvData As List(Of List(Of String))) As HeroDS

        Dim heroSet As New HeroDS()
        Dim heroTable As HeroDS.HeroDataTable = heroSet.Tables("Hero")

        For Each csvRow As List(Of String) In csvData

            Dim heroRow As HeroDS.HeroRow = heroTable.NewRow()

            For i As Integer = 0 To heroTable.Columns.Count - 1

                If heroTable.Columns(i).DataType = GetType(Int16) Then
                    heroRow(i) = CShort(csvRow(i))
                ElseIf heroTable.Columns(i).DataType = GetType(Boolean) Then
                    heroRow(i) = CBool(csvRow(i))
                Else
                    heroRow(i) = csvRow(i)
                End If
            Next

            heroTable.Rows.Add(heroRow)
        Next

        Return heroSet

    End Function

    Private Sub AddHeroColumns()

        Dim txtCell As DataGridViewTextBoxCell = New DataGridViewTextBoxCell
        Dim chkCell As DataGridViewCheckBoxCell = New bapDataGridViewCheckBoxCell

        For Each colHero As DataColumn In _heroDS.Tables("Hero").Columns

            Dim dgrCol As New DataGridViewColumn
            With dgrCol

                If colHero.DataType = GetType(Boolean) Then
                    .CellTemplate = chkCell
                Else
                    .CellTemplate = txtCell
                End If
                .Name = colHero.ColumnName
                .HeaderText = colHero.Caption
                .ValueType = colHero.DataType
                .DataPropertyName = colHero.ColumnName
                .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
            End With

            dgrHeroes.Columns.Add(dgrCol)
        Next

    End Sub

    Private Function PrepHeroDSForWriter(ByVal heroSet As HeroDS) As List(Of List(Of String))

        Dim heroData As New List(Of List(Of String))

        For Each dr As HeroDS.HeroRow In heroSet.Tables("Hero").Rows
            Dim rowList As New List(Of String)
            For Each dcol As DataColumn In heroSet.Tables("Hero").Columns
                rowList.Add(dr(dcol.ColumnName).ToString())
            Next
            heroData.Add(rowList)
        Next

        Return heroData

    End Function

    Private Function PrepHeroDSHeadersForWriter(ByVal heroSet As HeroDS) As List(Of String)

        Dim headers As New List(Of String)

        For Each dcol As DataColumn In heroSet.Tables("Hero").Columns
            headers.Add(dcol.Caption)
        Next

        Return headers

    End Function

#End Region

End Class